﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Truron.Purchase.Interface;
using Truron.Purchase.Api.Utility;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Truron.Purchase.Model;
using Newtonsoft.Json;
using Truron.Purchase.Utility;
using Newtonsoft.Json.Linq;

namespace Truron.Purchase.Api.Controllers
{

    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class Purchase_TestController : ControllerBase
    {
        private readonly IPurchase_TestService _Purchase_TestService;
        public Purchase_TestController(IPurchase_TestService Purchase_TestService)
        {
            _Purchase_TestService = Purchase_TestService;
        }
        [HttpPost]
        public IActionResult GetList(LayPageModel param)
        {
            var userToken = SysCenterServiceHelp.GetUser(HttpContext);
            int pageSize = Convert.ToInt32(param.limit), pageIndex = Convert.ToInt32(param.page);
            var Tuple = _Purchase_TestService.GetList(param.searchKey,  pageSize, pageIndex, param.searchJson);
            return Ok(JsonConvert.SerializeObject(new ResultData<object>() { data = Tuple.list, count = Tuple.count }));
        }
        [HttpPost]
        public IActionResult Save(RecParam param)
        {
            Purchase_Test entity = JsonConvert.DeserializeObject<Purchase_Test>(param.strEntity);
            var userToken = SysCenterServiceHelp.GetUser(HttpContext);
            int resultCode = _Purchase_TestService.Save(entity, userToken);
            return Ok(JsonConvert.SerializeObject(new ResultData(resultCode)));
        }
        public class RecParam
        {
            public string strEntity { get; set; }
        }
        [HttpPost]
        public IActionResult Del(RecParam param)
        {
            if (param.strEntity.Length == 0)
                return Ok(JsonConvert.SerializeObject(new ResultData(ErrorCodeConst.ParameterError)));
            List<int> ids = JsonConvert.DeserializeObject<List<int>>(param.strEntity);
            var userToken = SysCenterServiceHelp.GetUser(HttpContext);
            var resultCode = _Purchase_TestService.Del(ids, userToken);
            return Ok(JsonConvert.SerializeObject(new ResultData(resultCode)));
        }

    }
}